/**
 * @author Administrator
 * 多选择人员树
 */
var selectusertreecheckliandongwin='',liandongtree,selectusertreecheckliandongidtager='',selectusertreecheckliandongnametager='';
function initselectusertreecheckliandong(type){
	 var datasort = new Ext.data.Store();
	 var userrecord = new Ext.data.Record([
	 	{name:'name',type:'string'},
	 	{name:'id',type:'int'}
	 ]);
	 Ext.BLANK_IMAGE_URL=Ext.get('basePath').getValue()+"ajaxFramework/ext-2.1/resources/images/default/s.gif";
	 var url = Ext.get('basePath').getValue()+'tjsinfo/tjsoa/system/selectusertreecheck.do';
	 var load = new Ext.tree.TreeLoader({
	 	dataUrl:url
	 });
	 var root = new Ext.tree.AsyncTreeNode({
	 	id:'0',
	 	text:'天津利达粮油有限公司'
	 })
	 liandongtree = new Ext.tree.TreePanel({
	 		x:0,
	 		y:0,	
        	autoScroll: true,
        	animate: true,
        	containerScroll: true,
        	border: true,
	        loader:load,	 
	        anchor:'100% 100%'
	    });
	 liandongtree.setRootNode(root); 
	 liandongtree.on('checkchange', function(node, checked) {   
		node.on('expand',function(){
			node.attributes.checked = checked;   
			node.eachChild(function(child) {   
				child.ui.toggleCheck(checked);   
				child.attributes.checked = checked;   
				child.fireEvent('checkchange', child, checked);   
			});  
		});
		node.expand();  
		 
		 
	}, liandongtree); 
	 liandongtree.on('beforeload',function(node){ 
		 liandongtree.loader.dataUrl=url+"?node="+node.id; //定义子节点的Loader 
	  }); 
	 liandongtree.on('load',function(node,event){
		if (type=='update'){
			node.expand(); 
			node.eachChild(function(child) {  
				child.expand(); 
			});
			inittreenode();			
		} 		 
	 });

	selectusertreecheckliandongwin = new Ext.Window({
		layout:'absolute',
		title:'人员选择',
		width:520,
		height:500,
		modal:true,
		closeAction:'hide',
		items:[liandongtree],
		buttonAlign:'right',
		buttons:[{
			text:'全选',
			handler:function(){
				var rootnode_ = liandongtree.getRootNode();
				rootnode_.eachChild(function(child) {   
					child.ui.toggleCheck(true);   
					child.attributes.checked = true;
					child.fireEvent('checkchange', child, true);   
				});  			
			}
		},{
			text:'集团本部',
			handler:function(){
				var rootnode_ = liandongtree.getRootNode();
				rootnode_.eachChild(function(child) {
					if(child.attributes.isZhiShu == false){
						child.ui.toggleCheck(true);   
						child.attributes.checked = true;
						child.fireEvent('checkchange', child, true);
					}					   
				});  			
			}
		},{
			text:'确认',
			handler:function(datasort){
				var checkarray = liandongtree.getChecked(); 
				var username='',userid='';
				var indexname=1;
				for(var i = 0;i<liandongtree.getChecked().length;i++)
				{
					
					if(!liandongtree.getChecked()[i].attributes.isdepartment)
					{
						
						if(username.indexOf(','+liandongtree.getChecked()[i].text+',') == -1 && liandongtree.getChecked()[i].text!= liandongtree.getChecked()[0].text || username == ''){
							username+=liandongtree.getChecked()[i].text+',';						
							userid+=liandongtree.getChecked()[i].id+',';
						}
						
					}
				}
				$('#'+selectusertreecheckliandongidtager).attr('value',userid);
				$('#'+selectusertreecheckliandongnametager).attr('value',username);
				selectusertreecheckliandongwin.hide();
			}
		},{
			text:'取消',
			handler:function(){
				selectusertreecheckliandongwin.hide();		
			}
		}]
		
	})
//	inittreenode();
}

//显示窗体
function selectusertreecheckliandongwinshow(id,name,node){
	selectusertreecheckliandongidtager = id;
	selectusertreecheckliandongnametager = name;
	inittreenode();
	selectusertreecheckliandongwin.show();	
	selectusertreecheckliandongwin.findByType('treepanel')[0].root.expand();
//	var rootnode_ = tree.getRootNode();	
}

function inittreenode(){
	//tree  是个Ext.tree.TreePanel对象
    var rootnodes = liandongtree.getRootNode();   //获取主节点
	var ids = ","+$('#'+selectusertreecheckliandongidtager).attr('value');
    findchildnode(rootnodes,ids);  //开始递归
     
    function findchildnode(node,ids){
        var childnodes = node.childNodes;
        for(var i=0;i<childnodes.length;i++){ //从节点中取出子节点依次遍历            
        	var rootnode = childnodes[i];
        	rootnode.exp
            if (ids.indexOf(','+rootnode.id+',') >= 0){
                rootnode.ui.toggleCheck(true);   
                rootnode.attributes.checked = true;
                rootnode.fireEvent('checkchange', rootnode, true);
            }
            if(rootnode.childNodes.length>0){  //判断子节点下是否存在子节点，个人觉得判断是否leaf不太合理，因为有时候不是leaf的节点也可能没有子节点
                findchildnode(rootnode,ids);    //如果存在子节点  递归
            }    
        }
    }
}
